#ifndef 辅助类_H
#define 辅助类_H

#include <iostream>
#include <pthread.h>
#include <dirent.h>
#include <regex.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <vector>
#include <map>
#include <cstdio>
#include <stdio.h>
#include <thread>
#include <atomic>
#include <cmath>
#include <chrono>
#include <imgui.h>
#include "./CPUaffinity/timer.h"
#include "obfuscate.h"

#define PI 3.141592653589793238

//泛滥频道 https://t.me/YTNB520
class Kernel {
private:
    int has_upper = 0;
	int has_lower = 0;
	int has_symbol = 0;
	int has_digit = 0;
    int fd;
    pid_t pid;
    typedef struct _COPY_MEMORY {
        pid_t pid;
        uintptr_t addr;
        void* buffer;
        size_t size;
    } COPY_MEMORY, *PCOPY_MEMORY;

    typedef struct _MODULE_BASE {
        pid_t pid;
        char* name;
        uintptr_t base;
    } MODULE_BASE, *PMODULE_BASE;

    enum OPERATIONS {
        OP_INIT_KEY = 0x800,
        OP_READ_MEM = 0x801,
        OP_WRITE_MEM = 0x802,
        OP_MODULE_BASE = 0x803,
        OP_HIDE_PROCESS = 0x804,
    };
public:
    int get_dev();
    Kernel();
    ~Kernel();
    void 初始化读写(int pid);
    bool readv(uintptr_t addr, void *buffer, size_t size);
    bool writev(uintptr_t addr, void *buffer, size_t size);
    template<typename T>
    T Read(uintptr_t address);
    uintptr_t get_module_base(char* name);
    uintptr_t getPtr64(uintptr_t addr);
    uintptr_t getPtr32(uintptr_t addr);
    float getFloat(uintptr_t addr);
    int getDword(uintptr_t addr);
    int WriteDword(long int addr, int value);
    float WriteFloat(long int addr, float value);
    void writefloat(unsigned long addr,float data);
    int getPID(const char* packageName);
    void getUTF8(char * buf, unsigned long namepy);
    bool init_key(char* key);
    char *driver_path();
    int symbol_file(const char *filename);
};

#include "Draw.h"
#include "imgui_impl_opengl3.h"
#include "结构体.h"
#include "骨骼.hpp"


class StringFloatMap {
private:
    map<string, vector<float>> data;

public:
    void add(string key, float value1, float value2) {
        vector<float> values;
        values.push_back(value1);
        values.push_back(value2);
        data[key] = values;
    }

    void remove(string key) {
        data.erase(key);
    }
    
    bool exists(string key) {
        return data.find(key) != data.end();
    }
    
    string calculateKey(float 坐标X, float 坐标Y) {
        int maps = 999;
        string 键名;
        for (const auto& pair : data) {
            const vector<float>& values = pair.second;
            int result = (int)sqrt(pow(坐标X - values[0], 2) + pow(坐标Y - values[1], 2))*0.01;
            //printf("测试值:%f \n",result);
            if (result < maps) {
                //return pair.first;
                maps = result;
                键名 = pair.first;
                //break;
            }
        }
        if (maps < 20) {
          return 键名;
        }
        return "";  // 如果没有满足条件的键名，则返回空字符串
    }
};


class Timer {
private:
    std::map<std::string, int> timers;

public:
    void addTimer(const std::string& name, int seconds) {
        if (timers.find(name) == timers.end()) {
            timers[name] = seconds;
        }
    }

    void updateTimers() {
        for (auto& timer : timers) {
            timer.second++;
        }
    }

    void checkAndRemoveTimers() {
        for (auto it = timers.begin(); it != timers.end();) {
            if (it->second == 1500) {
                it = timers.erase(it);
            } else {
                ++it;
            }
        }
    }

    int getTimerSeconds(const std::string& name) {
        auto it = timers.find(name);
        if (it != timers.end()) {
            return it->second;
        }
        return -1;
    }

    void removeTimer(const std::string& name) {
        timers.erase(name);
    }

    bool hasTimer(const std::string& name) { //判断是否存在
        return timers.find(name) != timers.end();
    }

    void renameTimer(const std::string& oldName, const std::string& newName) {
        if (timers.find(oldName) != timers.end()) {
            timers[newName] = timers[oldName];
            timers.erase(oldName);
        }
    }
};


class 计算{
public:
    int 计算距离(D3DVector 自身坐标,D3DVector 对方坐标);
//    D2DVector 计算屏幕坐标2(float matrix[16], D3DVector obj, float PX, float PY);
    D4DVector 计算屏幕坐标(float 矩阵[16],D3DVector 人物坐标,float px,float py);
    D2DVector 计算屏幕坐标2(float 矩阵[16],D3DVector 人物坐标,float px,float py);
    float 计算屏幕距离(D2DVector &坐标,float px,float py);
    骨骼数据 计算骨骼(float (&矩阵)[16],D3DVector (&骨骼)[17],float px,float py);
    D2DVector rotateCoord(float angle, float objRadar_x, float objRadar_y);
    D2DVector rotateCoord(D3DVector Enemy, D3DVector RealPerson);
   };
//绘图
class 绘图{
struct 颜色{
	ImColor 红色 = ImColor(255,0,0,255);
	ImColor 白色 = ImColor(255,255,255,255);
	ImColor 蓝色 = ImColor(0,0,255,255);
	ImColor 绿色 = ImColor(0,255,0,255);
	ImColor 黄色 = ImColor(255,255,0,255);
};
public:
	颜色 颜色;//颜色的类
	float PX,PY;
	float MIDDLE,BOTTOM,TOP;
    float left,right,top,top1,bottom;
    bool isAiming;
	void 初始化绘图(int X,int Y);
	void 初始化坐标(D4DVector &屏幕坐标,骨骼数据 &骨骼);
	void 绘制方框(bool isboot);
	void 绘制人数(int 人机, int 真人);
	void 绘制距离(int 距离, int 队伍);
	void 绘制射线(骨骼数据 &骨骼);
	void 绘制名字(string 名字, bool isboot, float 计时, bool 是否掐雷, char* 类名, int 阵营, int Bonecount);
	void 绘制血量(float 最大血量,float 当前血量,骨骼数据 &骨骼);
	void 绘制骨骼(骨骼数据 &骨骼, D4DVector &屏幕坐标, bool LineOfSightTo);
	void 绘制手持(int 手持, int 状态);
	void 绘制自瞄触摸范围(float 触摸范围,float 触摸范围X,float 触摸范围Y);
	void 绘制车辆(D4DVector 屏幕坐标, int 距离, int CarrierID);
	void 绘制加粗文本(float size, float x, float y, ImColor color, ImColor color1, const char* str);
    void 绘制字体描边(float size,int x, int y, ImVec4 color, const char* str);
    void RenderRadarScan(ImDrawList* draw_list, ImVec2 center, float radius, int numSegments, float& rotationAngle, float lineLength);
//   void 绘制瞄准信息();
};

class 驱动_Recoil {
public:
    float operator()(int weaponId);
};
class 驱动_GetWeaponId {
public:
    float operator()(int weaponId);
};

//绘制
class 绘制
{

struct ColorTable
{
  float 方框颜色[4] = {0.0,1.0,0.0,1.0};
  float 射线颜色[4] = {1.0,1.0,1.0,1.0};
  float 骨骼颜色[4] = {0.0,1.0,0.0,1.0};
//  float 血量颜色[4] = {0.0,1.0,0.0,1.0};  
//  float 阵营颜色[4] = {1.0,1.0,0.0,1.0};
//  float 距离颜色[4] = {1.0,1.0,1.0,1.0};  
//  float 名称颜色[4] = {1.0,1.0,1.0,1.0};
};

    public:
    void GetTouch();
    bool 自瞄触发(float 距离);
    void 驱动自瞄主线程();
    private:
    std::atomic<bool> runningTouchThread;
   // 
    自身数据 自身数据;
    骨骼* 骨骼;
    计算 计算;
 //   驱动_Recoil
//    驱动_GetWeaponId;
    float PX;
    float PY;

    



	public:	
	int 防录屏,自瞄模式,无后台开关;
	const char* Level(char *name);
	
	bool Winorlose = false;
	ImVec2 Pos;
    int winWidth = 684;
    int winHeith = 896;
    
    int 被瞄准对象数量 = 0;
    
    FILE *numSave = nullptr;

	ColorTable Colorset[2];//颜色配置
    int pid;
	bool 线程=0;//用于判断更新数据显示是否加载
//	float PX,PY;//绘制用的分辨率
	float 真实PX,真实PY;
	bool Validate;
	Kernel 读写;//创建读写结构体
	
	StringFloatMap 手雷类;
	Timer 计时器;//计时器
	
	char 悬浮窗标题[200];
	
	bool Shelter[14];
	
	地址 地址;
	开关 按钮;
//	骨骼 *骨骼;
	绘图 绘图;
	std::mutex mtx;
	自瞄 自瞄;
	自瞄信息 自瞄函数[100];
	// 瞄准信息 被瞄信息[100];//创建自身数据结构体
	对象地址 对象地址;//创建对象地址结构体
	对象信息 对象信息;//创建敌人信息结构体数组
	void 初始化绘制(string 包名,int 真实X,int 真实Y);	
    int findminat();
	void 更新地址数据();
	void 多线程更新地址();
	void 更新对象地址();
	void 更新对象数据();
	void 绘制载具信息();
	void 运行绘制();
	void 自瞄主线程();
	int Acquisitionsite();
	void hide_process();
	string getBoxName(int id);
	void OffScreen(ImDrawList *ImDraw, D4DVector Obj, float camear, ImU32 color, float Radius, float 距离);
	void 保存配置();
	void 读取配置();
	const char* getMaterialName(char *name);
	int Cloudcheck();
};


class 布局{
public:
// 布局UI
    void 开启悬浮窗();
    void 绘制悬浮窗();
    int 初始化程序();
};

#endif

